import React from "react";
import { Route, Redirect,useLocation} from "react-router-dom";
import RouterConfig from "./RouterConfig";
import ContentRouterConfig from "./ContentRouterConfig";
import {message} from 'antd';
import {isAuthenticated} from '../utils/auth'
import HomePage from "./HomePage";

function OssAuth(props){
   const location = useLocation();
    const pathname = location.pathname;
    const primaryRouter = RouterConfig.find(function(item) {
      return item.path === pathname;
    });
    const contentRouter =  ContentRouterConfig.find(function(item){
      return item.path === pathname;
    });
    const isLogin = isAuthenticated();
    if (pathname === "/"||pathname === "") {
      if (!isLogin){
        message.warning("请先登录");
        return <Redirect exact to="/login"></Redirect>;
      }
      }
      if (!(primaryRouter||contentRouter)) {
        return <Redirect exact to="/404" />;
      }
      if (isLogin) {
        if (pathname === "/login") {
          return <Redirect exact to="/"></Redirect>;
        } 
        else{
          if(primaryRouter){
            return <Route exact path={pathname} component={primaryRouter.component} />
          }
          if(contentRouter){
            return <Route exact path={pathname} component={HomePage} />
          }
        }
      } else {
        if (primaryRouter.auth) {
          message.warning("请先登录");
          return <Redirect exact to="/login" />;
        } else {
          return <Route path={pathname} component={primaryRouter.component} />
        }
      }
}
export default OssAuth;